In [1]:
from ipywidgets import StaticInteract, RangeWidget, RadioWidget
In [12]:
import warnings
warnings.filterwarnings('ignore')
In [2]:
from astropy import coordinates
from astropy import units as u
from astropy.io import fits
import aplpy
import pylab

dpath = '/Volumes/128gbdisk/w51/'
/Users/adam/virtual-python/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-intel.egg/mpl_toolkits/__init__.py:2: UserWarning: Module argparse was already imported from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.pyc, but /Users/adam/virtual-python/lib/python2.7/site-packages is being added to sys.path
  __import__('pkg_resources').declare_namespace(__name__)

In [3]:
hdu = fits.open(dpath+'W51Ku_BDarray_continuum_2048_both_uniform.hires.clean.image.fits')
for ii in [3,4]:
    for kk in ['CRVAL','CTYPE','CDELT','CRPIX','CUNIT','NAXIS']:
        k = kk+str(ii)
        if k in hdu[0].header:
            del hdu[0].header[k]
hdu[0].header['NAXIS'] = 2
hdu[0].data = hdu[0].data.squeeze()
In [4]:
hdu2 = fits.open(dpath+'H2CO_22_Ku_D_taucube_rebin.fits')

yy,xx = np.indices(hdu2[0].data.shape[1:])
rr = ((xx-hdu2[0].data.shape[2]/2-1)**2+(yy-hdu2[0].data.shape[1]/2-1)**2)**0.5
mask = rr < 200
nanmask = np.zeros_like(mask,dtype='float')
nanmask[True-mask] = np.nan

hdu2[0].data += nanmask
In [5]:
h=hdu2[0].header
velocities = 1e-3 * ((np.arange(h['NAXIS3'])+1-h['CRPIX3'])*h['CDELT3']+h['CRVAL3'])
In [6]:
def make_plot(overlay_layer=0):
    fig=figure(figsize=(10,10))
    F = aplpy.FITSFigure(hdu,convention='calabretta',figure=fig)
    F.tick_labels.set_xformat('dd.dd')
    F.tick_labels.set_yformat('dd.dd')
    F.tick_labels.set_font(size=20)
    F.axis_labels.set_font(size=20)
    F.show_grayscale(stretch='arcsinh',vmin=-5e-4,vmax=0.011)
    F.recenter(290.92345,14.511772,radius=1.5/60.)

    F.show_contour(hdu2,levels=[0.03,0.08,0.12,15.0],colors=[(0.8,0.2,0.0,0.2),(0.8,0.2,0.0,0.4),(0.8,0.2,0.0,0.6)],
                   filled=True,
                   slices=[overlay_layer])

    kms = velocities[overlay_layer]
    F.add_label(0.9,0.9,"v=%0.1f km/s" % kms,relative=True, color='r', size=20)
    
    return fig
In [7]:
StaticInteract(make_plot,
               overlay_layer=RangeWidget(0,20,1))
/Users/adam/virtual-python/lib/python2.7/site-packages/numpy-1.8.0-py2.7-macosx-10.6-intel.egg/numpy/ma/core.py:778: RuntimeWarning: underflow encountered in multiply
  return umath.absolute(a) * self.tolerance >= umath.absolute(b)
/Users/adam/virtual-python/lib/python2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.6-intel.egg/matplotlib/pyplot.py:415: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_num_figures`).
  max_open_warning, RuntimeWarning)

Out[7]:

Source: http://www.adamgginsburg.com/InteractiveFigure.ipynb

(data's too big to share, plus it's not published or properly reduced yet...)

In [11]:
#!ipython nbconvert InteractiveFigure.ipynb --to html
!ipython nbconvert InteractiveFigure.ipynb --to slides --template output_toggle --reveal-prefix="//cdn.jsdelivr.net/reveal.js/2.4.0/"
/Users/adam/virtual-python/lib/python2.7/site-packages/Pygments-1.6-py2.7.egg/pygments/plugin.py:39: UserWarning: Module argparse was already imported from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.pyc, but /Users/adam/virtual-python/lib/python2.7/site-packages is being added to sys.path
  import pkg_resources
[NbConvertApp] Using existing profile dir: u'/Users/adam/.ipython/profile_default'
Logging to /Users/adam/work/w51/ipython_log_2013-12-06.py
[NbConvertApp] Converting notebook InteractiveFigure.ipynb to slides
[NbConvertApp] Support files will be in InteractiveFigure_files/
[NbConvertApp] Loaded template output_toggle.tpl
[NbConvertApp] Writing 10373516 bytes to InteractiveFigure.slides.html

In []: